package com.example.mykm2_7;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;

public class MainActivity extends AppCompatActivity {

    public static DataInputStream dataInputStream = null; //输入流
    public static DataOutputStream dataOutputStream =null;//输出流
    private byte[] buff = new byte[10];  //接收缓冲区
    TextView pmTV;
    TextView yyTV;
    TextView tfTV;
    TextView ster;
    public static int wd;
    public static int sd;
    private Button btn_conn;
    private Button btn_close;
    private EditText et_ip;
    private EditText et_port;
    private  String ip;//服务器IP地址
    private int port;//服务器端口
    public static Socket socket = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pmTV=this.findViewById(R.id.pmtv);
        yyTV=this.findViewById(R.id.yytv);
        tfTV=this.findViewById(R.id.tftv);
        ster=this.findViewById(R.id.ster);
        et_ip=findViewById(R.id.et_ip);
        et_port=findViewById(R.id.et_port);

        btn_conn = (Button) findViewById(R.id.btn_conn);
        btn_close = (Button) findViewById(R.id.btn_colse);
    btn_conn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ster.setText("已连接服务器！");
            startThread();
        }
    });
        btn_close.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ster.setText("已断开服务器！");
        }
    });
}
    private void startThread()
    {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ip=et_ip.getText().toString().trim();
                port=Integer.parseInt(et_port.getText().toString().trim());
                try {
                    socket = new Socket(ip,port);
                    dataInputStream = new DataInputStream(socket.getInputStream());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                while(socket!=null)
                {
                    try{
                        int len = dataInputStream.read(buff);
                        if(len==8 && buff[0]==(byte)0x55 && buff[1]==(byte)0xAA && buff[2]==(byte)0x00 && buff[3]==(byte)0x18)
                        {
                            wd = buff[4]*256 + buff[5];
//                            sd = buff[6]*256 + buff[7];
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                        pmTV.setText("PM2.5： "+wd);
                                }
                            });
                        }
                        if(len==8 && buff[0]==(byte)0x55 && buff[1]==(byte)0xAA && buff[2]==(byte)0x00 && buff[3]==(byte)0x2A)
                        {
                            wd = buff[4]*256 + buff[5];
//                            sd = buff[6]*256 + buff[7];
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    yyTV.setText("一氧化碳浓度： "+wd);
                                }
                            });
                        }
                        if(len==8 && buff[0]==(byte)0x55 && buff[1]==(byte)0xBB && buff[2]==(byte)0x00 && buff[3]==(byte)0x00)
                        {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if(buff[4]==(byte)0x01)
                                        tfTV.setText("通风设备：开" );
                                    else
                                        tfTV.setText("通风设备：关");

                                }
                            });
                        }
                    }catch (IOException e){
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}